<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use DB;
class AgentController extends Controller{
    //旗下代理商列表
    public function getIndex(Request $request){
        //获取登录的代理商id
        // $agentId = session('agentId');
        $agentId = $request->cookie('agentId');
        //查询父级代理商path
        $path = DB::table('sa_agent') -> select('path') -> where(['id'=>$agentId]) -> first();
        $len = substr_count($path['path'],',');
        $data = self::allAgent($agentId);
        foreach ($data as $k=>$v){
            $data[$k]['agent_name'] = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',substr_count($v['path'],',')-$len)."<img src='/admin/images/company.png'>&nbsp;&nbsp;".$v['agent_name'];
        }
        /*dd($data);*/
        return view('admin.agent.index',['data'=>$data]);
    }

    //待审批
    public function getApproval(Request $request){
        //获取登录的代理商id
        // $agentId = session('agentId');
        $agentId = $request->cookie('agentId');
        //查询审批代理商表
        $data = DB::table('sa_approve') -> where(['agent_pid'=>$agentId]) -> get();
        foreach ($data as $k=>$v){
            $data[$k]['agent_name'] = "&nbsp;&nbsp;&nbsp;&nbsp;<img src='/admin/images/company.png'>&nbsp;&nbsp;".$v['agent_name'];
        }
        return view('admin.agent.approval',['data'=>$data]);
    }

    //同意代理处理方法
    public function postAgree(Request  $request){
        //获取ajax数据
        $approveId = $request -> get('approveId');
        //修改approve表记录
        $row = DB::table('sa_approve') -> where(['id'=>$approveId]) -> update(['status'=>1]);
        if($row){
            //已同意代理,将申请代理商表的记录插入到代理商表
            $data = DB::table('sa_approve')
                -> select('account','password','linkman','phone','agent_name','agent_pid')
                -> where(['id'=>$approveId])
                -> first();
            //查询agent表,获取父级代理商path
            $arr = DB::table('sa_agent') -> select('path') -> where(['id'=>$data['agent_pid']]) -> first();
            //整理需要插入的数据
            $data['pid'] = $data['agent_pid'];
            $data['path'] = $arr['path'].$data['agent_pid'].',';
            unset($data['agent_pid']);
            //将同意代理的记录插入agent表和admin表
            //dd($data);
            $insertId = DB::table('sa_agent') -> insertGetId($data);
            if($insertId){
                //同意成为代理商
                //将账号,密码,联系人,手机号信息插入admin表
                unset($data['agent_name']);
                unset($data['pid']);
                unset($data['path']);
                $data['agent_id'] = $insertId;
                $adminId = DB::table('sa_admin') -> insertGetID($data);
                if($adminId){
                    $message['status'] = '1';
                    $message['msg'] = '已同意';
                }else{
                    $message['status'] = '-2';
                    $message['msg'] = 'admin表操作失败';
                }
            }else{
                //插入失败
                $message['status'] = '-1';
                $message['msg'] = '操作失败';
            }
        }else{
            //状态已经为1
            $message['status'] = '-2';
            $message['msg'] = '已通过审批,请勿重复操作';
        }
        echo json_encode($message);
    }

    //不同意代理处理方法
    public function postDisagree(Request  $request){
        //获取ajax数据
        $approveId = $request -> get('approveId');
        //修改approve表记录
        $row = DB::table('sa_approve') -> where(['id'=>$approveId]) -> update(['status'=>2]);
        if($row){
            //修改状态成功
            $message['status'] = '1';
            $message['msg'] = '审批不通过';
        }else{
            //修改失败
            $message['status'] = '-1';
            $message['msg'] = '操作失败或已经是未通过审批状态';
        }
        echo json_encode($message);
    }

    //获取自己旗下的所有代理商
    static public function allAgent($agentId,&$brr=[]){
        $arr = DB::table('sa_agent') -> where(['pid'=>$agentId])->get();
        if($arr != null){
            foreach ($arr as $v){
                $brr[$v['id']] = $v;
                self::allAgent($v['id'],$brr);
            }
        }
        return $brr;
    }

}
